package system_smtp_log_attachment_dao

import (
	"gitee.com/Luna-CY/Golang-Project-Template/internal/context"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/interface/dao"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/logger"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
	"gorm.io/gorm"
)

func (cls *SystemSmtpLogAttachment) TakeSystemSmtpLogAttachmentByCondition(ctx context.Context, options ...dao.SystemSmtpLogAttachmentOption) (*model.SystemSmtpLogAttachment, errors.Error) {
	if 0 == len(options) {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("IDSSLA_NT.SSLA_NT.TSSLABC_ON.150357 options is empty")

		return nil, errors.ErrorServerInternalError("IDSSLA_NT.SSLA_NT.TSSLABC_ON.170357", "异常的传入参数")
	}

	var session = cls.GetDb(ctx).Model(new(model.SystemSmtpLogAttachment))

	var metadata = dao.NewMetadata()
	for _, option := range options {
		var err errors.Error
		if session, err = option(session, metadata); nil != err {
			if err.IsType(errors.ErrorTypeRecordNotFound) {
				return nil, err.Relation(errors.ErrorRecordNotFound("IDSSLA_NT.SSLA_NT.TSSLABC_ON.260357"))
			}

			return nil, err.Relation(errors.ErrorServerInternalError("IDSSLA_NT.SSLA_NT.TSSLABC_ON.290357", "配置选项失败"))
		}
	}

	var systemSmtpLogAttachment *model.SystemSmtpLogAttachment
	if err := session.Take(&systemSmtpLogAttachment).Error; nil != err {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, errors.ErrorRecordNotFound("IDSSLA_NT.SSLA_NT.TSSLABC_ON.360357")
		}

		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("IDSSLA_NT.SSLA_NT.TSSLABC_ON.390357 take systemSmtpLogAttachment options failed, err %v", err)

		return nil, errors.ErrorServerInternalError("IDSSLA_NT.SSLA_NT.TSSLABC_ON.410357", "获取数据失败")
	}

	return systemSmtpLogAttachment, nil
}
